/** 
 * File-Name:ArticleRepository.java
 *
 * Created on 2011-6-2 下午03:48:12
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import neoutil.CommonEntity;
import neoutil.CommonService;
import neoutil.DaoSupport;
import neoutil.Page;

import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.Logger;

import site.dao.ArticleDao;
import site.dao.PaperDao;
import site.dao.UserDao;
import site.entity.Article;
import site.entity.Paper;
import site.entity.Role;
import site.entity.User;

/**
 * Description: 用户Article的各种存储相关操作。
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午03:48:12
 */
public class PaperRepository extends CommonService {
	private static Logger log = Logger.getLogger(PaperRepository.class);

	public String save(Paper paper) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();
			PaperDao paperDao = new PaperDao(connection);
			paperDao.save(paper);
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存"+paper.getClass().getName()+"对象出现异常！！");
			e.printStackTrace();
			return FAIL;
		} finally {
			closeConnection(connection);
		}
	}

	public String update(Paper paper) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();
			PaperDao paperDao = new PaperDao(connection);
			paperDao.update(paper);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return FAIL;
		} finally {
			closeConnection(connection);
		}
	}

	public String deletePaper(Paper paper) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();
			PaperDao paperDao = new PaperDao(connection);
			paperDao.delete(paper);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return FAIL;
		} finally {
			closeConnection(connection);
		}
	}

	public Paper getPaperByUuid(String uuid) {
		Connection connection = null;

		try {
			connection = DaoSupport.getConnection();
			PaperDao paperDao = new PaperDao(connection);

			Paper paper = paperDao.getByUuid(uuid);
			return paper;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			closeConnection(connection);
		}
	}

	/**
	 * @method getLatestPapersByCid
	 * @info author:陈亚鹏  date:2012-9-28 下午03:58:56 version:1.0
	 * @param cid
	 * @param topNum
	 * @return List<Paper>
	 */
	public List<Paper> getLatestPapersByCidAndType(int cid,String type, int topNum) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();
			PaperDao dao = new PaperDao(connection);

			String sql = "SELECT * FROM paper WHERE cid=? AND type=? AND entityStatus="
					+ CommonEntity.ENTITY_STATUS_NORMAL
					+ " ORDER BY publishDate DESC LIMIT 0,?";

			Object[] params = {cid, type, topNum };
			List<Paper> papers = dao.find(sql, params);
			return papers;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			closeConnection(connection);
		}
	}	

	/**
	 * 由栏目号和文章所属类型找到对应文章列表
	 * @method getPapersByCidAndType
	 * @info author:陈亚鹏  date:2012-9-28 下午07:44:13 version:1.0
	 * @param page
	 * @param cid
	 * @param type 可以为Null 查找出所有
	 * @return Page<Paper>
	 */
	public Page<Paper> getPapersByCidAndType(Page<Paper> page, int cid, String type) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();
			PaperDao dao = new PaperDao(connection);
			String sql = "SELECT * FROM paper WHERE cid=? " + (type!=null?" AND type=? ":"")
					+ " AND entityStatus="
					+ CommonEntity.ENTITY_STATUS_NORMAL
					+ " ORDER BY publishDate DESC";
			Object[] params = { cid,type};
			if(type==null)params = new Object[]{cid};
			
			page = dao.find(page, sql, params);
			if (page == null || page.getResult() == null || page.getResult().size() == 0) {
				return null;
			} 
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			closeConnection(connection);
		}
	}
}
